<?php
/**
 * @author GhostWei
 * @date Oct 14, 2012
 */
class resource_advertise_item_concept extends system_concept_abstract {
	
	public function init() {
		$this->setRowClass('resource_advertise_row');
		$this->setModel('resource_advertise_item_model', 'item');
		$this->setJoinLeftModel('resource_file_model', 'file.hash = item.hash', 'file');
		$this->setJoinLeftModel('resource_link_model', 'link.key = item.link', 'link');
		$this->setJoinModel('resource_advertise_model', 'advertise.id = item.advertise_id', 'advertise');
		$this->setModelCols('item', array('id', 'advertise_id', 'display_order', 'is_layout', 'hash', 'link'));
		$this->setModelCols('item', array('script', 'script_body', 'style', 'style_body', 'description'));
		$this->setModelCols('advertise', array('name', 'width', 'height'));
		$this->setModelCols('file', array('extension'));
		$this->setModelCols('link', array('key', 'url', 'click'));
	}
	
	public function save(resource_advertise_row $row) {
		if(!$row->getAdvertiseId()) {
			throw new resource_exception('advertise id can not be empty.');
		}
		if(!$row->getDisplayOrder()) {
			$row->setDisplayOrder(0);
		}
		if(!$row->getScript() && !$row->getUrl() && !$row->getScriptBody()) {
			throw new resource_exception('script src, script body, default url must be set to at least one.');
		}
		$row->setIsLayout($row->get('is_layout') ? 1 : 0);
		$langList = $row->getLangList();
		if(empty($langList)) {
			throw new resource_exception('display on locale must be set one at least.');
		}
		$deviceList = $row->getDeviceList();
		if(empty($deviceList)) {
			throw new resource_exception('display on device must be set one at least.');
		}
		$key = '';
		if($row->getUrl()) {
			$key = system_encrypt::shortUrl($row->getUrl());
			$row->setKey($key);
		}
		$row->setLink($key);
		if($row->getId()) {
			$this->getModel('item')->update($row->getModelData('item'), array('id = ?' => $row->getId()));
		} else {
			$id = $this->getModel('item')->insert($row->getModelData('item'));
			$row->setId($id);
		}
		if($key) {
			if(!$this->getModel('link')->fetchRow(array('`key` = ?' => $key))) {
				$row->setClick(0);
				$this->getModel('link')->insert($row->getModelData('link'));
			}
		}
		
		$langModel = new resource_advertise_lang_model();
		$langModel->delete(array('item_id = ?' => $row->getId()));
		foreach($langList As $key => $lang) {
			if(!isset($lang['start_time']) || !isset($lang['end_time'])) {
				throw new resource_exception('start time and end time can not be empty.');
			}
			if($lang['start_time'] > $lang['end_time']) {
				throw new resource_exception('start time must be before end time.');
			}
			$langModel->insert(array(
						'item_id' => $row->getId(),
						'lang' => strtolower($key),
						'start_time' => $lang['start_time'],
						'end_time' => $lang['end_time']
					)
				);
		}
		$deviceModel = new resource_advertise_device_model();
		$deviceModel->delete(array('item_id = ?' => $row->getId()));
		foreach($deviceList As $device) {
			$deviceModel->insert(array('item_id' => $row->getId(), 'device_key' => strtoupper($device)));
		}
		return $row;
	}
	
	public function delete(resource_advertise_row $row, $options = array()) {
		$model = new resource_advertise_lang_model();
		$model->delete(array('item_id = ?' => $row->getId()));
		$model = new resource_advertise_device_model();
		$model->delete(array('item_id = ?' => $row->getId()));
		parent::delete($row);
	}
	
}
